/*********************
* Author:Dzlua
* QQ:505544956
* time:2016/11/30
*/
#include "LuaControl.h"
#include "../../utilities/Log.h"

#ifdef EDUI_USE_LUAPLUS

EDUI_NAMESPACE_BEGIN

#ifdef EDUI_TEST_NEW_DELETE
int LuaControl::m_newdeleteNum = 0;
int LuaControl::_GetNewDeleteNum()
{
    return m_newdeleteNum;
}
#endif

void* LuaControl::GetInterface(void* data)
{
    if (!data) return 0;
    LuaControl* ctrl = new LuaControl((Control*)data);
#ifdef EDUI_TEST_NEW_DELETE
    ++m_newdeleteNum;
    Log("[LuaControl] new LuaControl : %d, %d", ctrl, data);
#endif
    return ctrl;
}

void LuaControl::Release()
{
#ifdef EDUI_TEST_NEW_DELETE
    --m_newdeleteNum;
    Log("[LuaControl] delete LuaControl : %d, %d, %s", this, m_cobj, this->GetName());
#endif
    delete this;
}

void* LuaControl::GetCObj()
{
    return m_cobj;
}

LuaControl::LuaControl(Control* ctrl)
{
    m_cobj = ctrl;
}

LuaControl::~LuaControl()
{}

//----------------------------------------//

const char* LuaControl::GetName()
{
    return m_cobj->GetName();
}

bool LuaControl::SetName(const char* name)
{
    return m_cobj->SetName(name);
}

int LuaControl::GetPos(LuaState *L)
{
    Point data = m_cobj->GetPos();
    LuaObject obj = LuaBaseNS::ToLuaObj(L, &data);
    obj.Push(L);
    return 1;
}

bool LuaControl::SetPos(int x, int y)
{
    Point pt(x, y);
    return m_cobj->SetPos(&pt);
}

int LuaControl::GetSize(LuaState *L)
{
    Size data = m_cobj->GetSize();
    LuaObject obj = LuaBaseNS::ToLuaObj(L, &data);
    obj.Push(L);
    return 1;
}

bool LuaControl::SetSize(int w, int h)
{
    Size sz(w, h);
    return m_cobj->SetSize(&sz);
}

int LuaControl::GetRect(LuaState *L)
{
    Rect data = m_cobj->GetRect();
    LuaObject obj = LuaBaseNS::ToLuaObj(L, &data);
    obj.Push(L);
    return 1;
}

bool LuaControl::SetRect(int x, int y, int w, int h)
{
    Rect rc(x, y, w, h);
    return m_cobj->SetRect(&rc);
}

int LuaControl::GetFixedPos(LuaState *L)
{
    Point data = m_cobj->GetFixedPos();
    LuaObject obj = LuaBaseNS::ToLuaObj(L, &data);
    obj.Push(L);
    return 1;
}

bool LuaControl::SetFixedPos(int x, int y)
{
    Point pt(x, y);
    return m_cobj->SetFixedPos(&pt);
}

int LuaControl::GetFixedSize(LuaState *L)
{
    Size data = m_cobj->GetFixedSize();
    LuaObject obj = LuaBaseNS::ToLuaObj(L, &data);
    obj.Push(L);
    return 1;
}

bool LuaControl::SetFixedSize(int w, int h)
{
    Size sz(w, h);
    return m_cobj->SetFixedSize(&sz);
}

int LuaControl::GetFixedRect(LuaState *L)
{
    Rect data = m_cobj->GetFixedRect();
    LuaObject obj = LuaBaseNS::ToLuaObj(L, &data);
    obj.Push(L);
    return 1;
}

bool LuaControl::SetFixedRect(int x, int y, int w, int h)
{
    Rect rc(x, y, w, h);
    return m_cobj->SetFixedRect(&rc);
}

int LuaControl::GetPadding(LuaState *L)
{
    Rect1 data = m_cobj->GetPadding();
    LuaObject obj = LuaBaseNS::ToLuaObj(L, &data);
    obj.Push(L);
    return 1;
}

bool LuaControl::SetPadding(int l, int t, int r, int b)
{
    Rect1 rc1(l, t, r, b);
    return m_cobj->SetPadding(&rc1);
}

bool LuaControl::IsFloat()
{
    return m_cobj->IsFloat();
}

void LuaControl::SetFloat(bool bFloat)
{
    m_cobj->SetFloat(bFloat);
}

bool LuaControl::IsVisible()
{
    return m_cobj->IsVisible();
}

void LuaControl::SetVisible(bool bVisible)
{
    m_cobj->SetVisible(bVisible);
}

bool LuaControl::IsEnabled()
{
    return m_cobj->IsEnabled();
}

void LuaControl::SetEnabled(bool bEnable)
{
    m_cobj->SetEnabled(bEnable);
}

bool LuaControl::IsMouseEnabled()
{
    return m_cobj->IsMouseEnabled();
}

void LuaControl::SetMouseEnabled(bool bEnable)
{
    m_cobj->SetMouseEnabled(bEnable);
}

bool LuaControl::IsKeyboardEnabled()
{
    return m_cobj->IsKeyboardEnabled();
}

void LuaControl::SetKeyboardEnabled(bool bEnable)
{
    m_cobj->SetKeyboardEnabled(bEnable);
}

bool LuaControl::IsFocused()
{
    return m_cobj->IsFocused();
}

void LuaControl::SetFocus()
{
    m_cobj->SetFocus();
}

void LuaControl::KillFocus()
{
    m_cobj->KillFocus();
}

bool LuaControl::IsDragWindow()
{
    return m_cobj->IsDragWindow();
}

bool LuaControl::IsInRect(int x, int y)
{
    return m_cobj->IsInRect(x, y);
}

int LuaControl::GetWindow(LuaState *L)
{
    IWindow* wnd = m_cobj->GetWindow();
    LuaObject obj = LuaHelperNS::GetFromObjs(L, wnd);
    obj.Push(L);
    return 1;
}

int LuaControl::GetParent(LuaState *L)
{
    Control* ctrl = m_cobj->GetParent();
    LuaObject obj = LuaHelperNS::GetFromObjs(L, ctrl);
    obj.Push(L);
    return 1;
}

void LuaControl::Invaldate()
{
    m_cobj->Invaldate();
}

bool LuaControl::LoadLayout(const char* xmlstr)
{
    return m_cobj->LoadLayout(xmlstr);
}

int LuaControl::SetCursor(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    ICursor* cursor = (ICursor*) LuaHelperNS::GetCObjectData(args[2]);
    m_cobj->SetCursor(cursor);
    return 0;
}

int LuaControl::GetCursor(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    ICursor* cursor = m_cobj->GetCursor();
    LuaObject obj = LuaHelperNS::GetFromObjs(L, cursor);
    obj.Push(L);
    return 1;
}

void LuaControl::SetColor(const char* colorstr)
{
    return m_cobj->SetColor(colorstr);
}

int LuaControl::GetColor(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsString()){
        const Color* pData = m_cobj->GetColor(args[2].GetString());
        LuaObject obj = LuaBaseNS::ToLuaObj(L, pData);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

void LuaControl::SetImage(const char* imagestr)
{
    return m_cobj->SetImage(imagestr);
}

void LuaControl::SetStatusImage(const char* statusimagestr)
{
    return m_cobj->SetStatusImage(statusimagestr);
}

int LuaControl::GetImage(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsString()){
        const ImageData *data = m_cobj->GetImage(args[2].GetString());
        LuaObject obj = LuaBaseNS::ToLuaObj(L, data);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int LuaControl::FindControl(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsString()){
        Control* ctrl = m_cobj->FindControl(args[2].GetString());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, ctrl);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int LuaControl::FindControlByTabID(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsInteger()){
        Control* ctrl = m_cobj->FindControlByTabID(args[2].GetInteger());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, ctrl);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

int LuaControl::FindParent(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsString()){
        Control* ctrl = m_cobj->FindParent(args[2].GetString());
        LuaObject obj = LuaHelperNS::GetFromObjs(L, ctrl);
        obj.Push(L);
    } else {
        L->PushNil();
    }
    return 1;
}

void LuaControl::SetTabID(int id)
{
    m_cobj->SetTabID(id);
}

int LuaControl::GetTabID()
{
    return m_cobj->GetTabID();
}

int LuaControl::SetAnimation(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    Animation* animation = (Animation*) LuaHelperNS::GetCObjectData(args[2]);
    m_cobj->SetAnimation(animation);
    return 0;
}

int LuaControl::GetAnimation(LuaState *L)
{
    Animation* animation = m_cobj->GetAnimation();
    LuaObject obj = LuaHelperNS::GetFromObjs(L, animation);
    obj.Push(L);
    return 1;
}

//----------------------------------------//

int LuaControl::SetEvent(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    bool bRet = false;
    if (args[2].IsString() && args[3].IsFunction()){
        bRet = LuaHelperNS::SetEventToObjs(L, m_cobj, args[2].GetString(), args[3]);
    }
    L->PushBoolean(bRet);
    return 1;
}

int LuaControl::RemoveEvent(LuaState *L)
{
    LuaStack args(L);
    //cause RegisterObjectFunctor,the first param must be args[2]
    if (args[2].IsString()){
        LuaHelperNS::RemoveEventFromObjs(L, m_cobj, args[2].GetString());
    }
    return 0;
}

//----------------------------------------//

void LuaControl::Register(LuaState *L)
{
    LuaObject obj = LuaHelperNS::AddToMetas(L, "Control");
    obj.SetObject("__index", obj);
    obj.RegisterDirect("__NewCObj__", &LuaControl::GetInterface);
    obj.RegisterObjectFunctor("SetEvent", &LuaControl::SetEvent);
    obj.RegisterObjectFunctor("RemoveEvent", &LuaControl::RemoveEvent);
    
    obj.RegisterObjectDirect("GetName", (LuaControl*)0, &LuaControl::GetName);
    obj.RegisterObjectDirect("SetName", (LuaControl*)0, &LuaControl::SetName);
    obj.RegisterObjectFunctor("GetPos", &LuaControl::GetPos);
    obj.RegisterObjectDirect("SetPos", (LuaControl*)0, &LuaControl::SetPos);
    obj.RegisterObjectFunctor("GetSize", &LuaControl::GetSize);
    obj.RegisterObjectDirect("SetSize", (LuaControl*)0, &LuaControl::SetSize);
    obj.RegisterObjectFunctor("GetRect", &LuaControl::GetRect);
    obj.RegisterObjectDirect("SetRect", (LuaControl*)0, &LuaControl::SetRect);
    obj.RegisterObjectFunctor("GetFixedPos", &LuaControl::GetFixedPos);
    obj.RegisterObjectDirect("SetFixedPos", (LuaControl*)0, &LuaControl::SetFixedPos);
    obj.RegisterObjectFunctor("GetFixedSize", &LuaControl::GetFixedSize);
    obj.RegisterObjectDirect("SetFixedSize", (LuaControl*)0, &LuaControl::SetFixedSize);
    obj.RegisterObjectFunctor("GetFixedRect", &LuaControl::GetFixedRect);
    obj.RegisterObjectDirect("SetFixedRect", (LuaControl*)0, &LuaControl::SetFixedRect);
    obj.RegisterObjectFunctor("GetPadding", &LuaControl::GetPadding);
    obj.RegisterObjectDirect("SetPadding", (LuaControl*)0, &LuaControl::SetPadding);

    obj.RegisterObjectDirect("IsFloat", (LuaControl*)0, &LuaControl::IsFloat);
    obj.RegisterObjectDirect("SetFloat", (LuaControl*)0, &LuaControl::SetFloat);
    obj.RegisterObjectDirect("IsVisible", (LuaControl*)0, &LuaControl::IsVisible);
    obj.RegisterObjectDirect("SetVisible", (LuaControl*)0, &LuaControl::SetVisible);
    obj.RegisterObjectDirect("IsEnabled", (LuaControl*)0, &LuaControl::IsEnabled);
    obj.RegisterObjectDirect("SetEnabled", (LuaControl*)0, &LuaControl::SetEnabled);
    obj.RegisterObjectDirect("IsMouseEnabled", (LuaControl*)0, &LuaControl::IsMouseEnabled);
    obj.RegisterObjectDirect("SetMouseEnabled", (LuaControl*)0, &LuaControl::SetMouseEnabled);
    obj.RegisterObjectDirect("IsKeyboardEnabled", (LuaControl*)0, &LuaControl::IsKeyboardEnabled);
    obj.RegisterObjectDirect("SetKeyboardEnabled", (LuaControl*)0, &LuaControl::SetKeyboardEnabled);
    obj.RegisterObjectDirect("IsFocused", (LuaControl*)0, &LuaControl::IsFocused);
    obj.RegisterObjectDirect("SetFocus", (LuaControl*)0, &LuaControl::SetFocus);
    obj.RegisterObjectDirect("KillFocus", (LuaControl*)0, &LuaControl::KillFocus);
    obj.RegisterObjectDirect("IsDragWindow", (LuaControl*)0, &LuaControl::IsDragWindow);
    obj.RegisterObjectDirect("IsInRect", (LuaControl*)0, &LuaControl::IsInRect);
    obj.RegisterObjectFunctor("GetWindow", &LuaControl::GetWindow);
    obj.RegisterObjectFunctor("GetParent", &LuaControl::GetParent);
    obj.RegisterObjectDirect("Invaldate", (LuaControl*)0, &LuaControl::Invaldate);
    obj.RegisterObjectDirect("LoadLayout", (LuaControl*)0, &LuaControl::LoadLayout);
    obj.RegisterObjectFunctor("SetCursor", &LuaControl::SetCursor);
    obj.RegisterObjectFunctor("GetCursor", &LuaControl::GetCursor);
    obj.RegisterObjectDirect("SetColor", (LuaControl*)0, &LuaControl::SetColor);
    obj.RegisterObjectFunctor("GetColor", &LuaControl::GetColor);
    obj.RegisterObjectDirect("SetImage", (LuaControl*)0, &LuaControl::SetImage);
    obj.RegisterObjectDirect("SetStatusImage", (LuaControl*)0, &LuaControl::SetStatusImage);
    obj.RegisterObjectFunctor("GetImage", &LuaControl::GetImage);
    obj.RegisterObjectFunctor("FindControl", &LuaControl::FindControl);
    obj.RegisterObjectFunctor("FindControlByTabID", &LuaControl::FindControlByTabID);
    obj.RegisterObjectFunctor("FindParent", &LuaControl::FindParent);
    obj.RegisterObjectDirect("SetTabID", (LuaControl*)0, &LuaControl::SetTabID);
    obj.RegisterObjectDirect("GetTabID", (LuaControl*)0, &LuaControl::GetTabID);
    obj.RegisterObjectFunctor("SetAnimation", &LuaControl::SetAnimation);
    obj.RegisterObjectFunctor("GetAnimation", &LuaControl::GetAnimation);

}

EDUI_NAMESPACE_END

#endif